﻿using Akso.Outbounds;
using Akso.Outbounds.Importing;
using Akso.Web.Areas.Inventory.Models.Outbounds;
using Akso.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Akso.Web.Areas.Inventory.Controllers
{
    public class OutboundsController : AbpZeroTemplateControllerBase
    {
        private readonly IOutboundAppService _outboundsAppService;
        private readonly IOutboundExcelImport _outboundExcelImport;
        public OutboundsController(IOutboundAppService outboundsAppService,
            IOutboundExcelImport outboundExcelImport)
        {
            _outboundsAppService = outboundsAppService;
            _outboundExcelImport = outboundExcelImport;
        }
        // GET: Inventory/Outbounds
        public ActionResult Index(OutboundsViewModelIndex model)
        {
            model.FilterText = Request.QueryString["filterText"];
            return View(model);
        }
        //弹出导入界面
        public PartialViewResult ImportModal(ImportModalViewModel model)
        {
            return PartialView("_ImportModal",model);
        }

        #region 上传附件(暂时放置临时文件夹中)
        /// <summary>
        /// 上传附件(暂时放置临时文件夹中)
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadOutboundFile()
        {
            try
            {
                var fileInfo = Request.Files["Filedata"];
                var saveUrl = System.Configuration.ConfigurationManager.AppSettings["UploadTempPath"];
                var dirPath = Server.MapPath(saveUrl);
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                string dirName = "Outbound";
                var fileName = fileInfo.FileName;
                var extension = Path.GetExtension(fileName);
                var fileExt = extension.ToLower();
                //创建文件夹
                dirPath += dirName + "/";
                saveUrl += dirName + "/";
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
                var filePath = dirPath + "/" + newFileName;
                fileInfo.SaveAs(filePath);
                var fileUrl = saveUrl + "/" + newFileName;

                return Content(fileUrl);
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }
        #endregion

        //导入数据库
        [HttpPost]
        public ActionResult OutboundImport(string tempFilePath)
        {
            JsonResult jsresult = new JsonResult();
            jsresult.Data = new { result = string.Empty };
            string path = string.Empty;

            try
            {
                string errorMsg = string.Empty;
                path = Server.MapPath(tempFilePath);
                errorMsg = _outboundExcelImport.ImportToDB(path);
                if (!string.IsNullOrEmpty(errorMsg))
                {
                    jsresult.Data = new { result = errorMsg };
                    return jsresult;
                }
            }
            catch (System.Exception ex)
            {
                jsresult.Data = new { result = "导入失败！" + ex.Message };
            }
            finally
            {
                //删除文件
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }
            }
            return jsresult;
        }

        //删除上传临时文件
        public ActionResult DeleteUploadTempFile(string filePath)
        {
            JsonResult jsresult = new JsonResult();
            jsresult.Data = new { result = string.Empty };
            try
            {
                string FinalPath = Server.MapPath(filePath);
                if (System.IO.File.Exists(FinalPath))
                {
                    System.IO.File.Delete(FinalPath);
                }
                jsresult.Data = new { result = string.Empty };
            }
            catch (Exception ex)
            {

                jsresult.Data = new { result = ex.Message };
            }
            return jsresult;
        }
    }
}